<?php

class NewServices extends CWidget {

    public $category;
    public $limit = 4;
    public $w = false;
    public $IDS = array(
        'remont-telefonov' => array(361, 365, 90133, 18683),
        'remont-planshetov' => array(11934, 11932, 20344, 23901),
        'remont-noutbukov' => array(3139, 4402, 3033, 23001),
        'remont-fotoapparatov' => array(9287, 9418, 9732, 9709),
//        'remont-chasov' => array(),
        'remont-televizorov' => array(21852, 22634, 24058, 24586),
        'elektronnye-knigi' => array(20830, 11644, 11725, 20794),
        'remont-monoblokov' => array(12218, 12168, 12262, 12145),
        'mp3-pleeery' => array(22718, 24110, 24133, 11226),
//        'remont-kompyuterov' => array()
    );

    public function run() {
        if (!$this->w)
            $this->setCategory();
//        echo $this->category;
        if (array_key_exists($this->category, $this->IDS)) {
            $order = array();
            $in = implode(',', $this->IDS[$this->category]);
            foreach ($this->IDS[$this->category] as $id) {
                $order[] = 'm."pageId" = ' . $id . ' DESC';
            }
            $order = implode(',', $order);
            $query = 'SELECT 
				(select "pageTitle" FROM mb_pages p WHERE p."pageId" = m."pageParent" ) || \' \' || m."pageTitle" as "pageTitle",
				m."pageUrl",
				m."pageImages"
			  FROM
				mb_pages m
			  WHERE
                          m."pageId" IN(' . $in . ') AND "pageType" = \'model\' AND "pageActive" = 1
			  ORDER BY ' . $order . '
			  LIMIT 4';
        } else
            $query = 'SELECT 
				(select "pageTitle" FROM mb_pages p WHERE p."pageId" = m."pageParent" ) || \' \' || m."pageTitle" as "pageTitle",
				m."pageUrl",
				m."pageImages"
			  FROM
				mb_pages m
			  WHERE
			 m."pageUrl" LIKE \'' . $this->category . '/%\' AND "pageType" = \'model\' AND "pageActive" = 1 ORDER BY "pageSort"
			  LIMIT 4';

        $servicesTop = Yii::app()->db->createCommand($query)->queryAll(TRUE);
        $this->render('newServices', array('servicesTop' => $servicesTop, 'category' => $this->category));
    }

    public function setCategory() {
        $a = explode('/', Controller::$pageData['pageUrl']);
        $this->category = $a[0];
    }

}
